using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mbl.ParameterizedTest.AcceptanceTests.CalculatorTests
{
    public class ErrorAggregator
    {        
        private readonly List<string> _errors = new List<string>();      

        public void AddError(string errorMessage)
        {
            _errors.Add(errorMessage);
        }

        public void FailIfAnyErrors()
        {
            if (_errors.Any())
            {
                var errorMessage = BuildErrorMessage();
                throw new AssertionFailedException(errorMessage);
            }
        }

        private string BuildErrorMessage()
        {
            var errorBuilder = new StringBuilder();
            errorBuilder.AppendLine($"The test failed with {_errors.Count} errors:");

            var nr = 1;
            foreach (var error in _errors)
            {
                errorBuilder.AppendLine($"  {nr.ToFixedNr(_errors.Count)}: {error}");
                nr++;
            }

            return errorBuilder.ToString();
        }
    }
}